package com.chahua.organized.controller;

import com.chahua.organized.common.DataResult;
import com.chahua.organized.entity.Department;
import com.chahua.organized.qo.DepartmentQo;
import com.chahua.organized.service.DepartmentService;
import com.chahua.organized.vo.DepartmentInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;


@Api(value = "部门管理")
@RestController
@RequestMapping("/api/v1")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    @ApiOperation(value = "新增部门")
    @PostMapping("/department")
    public DataResult addDepartment(@RequestBody @Valid DepartmentQo departmentQo) {
        departmentService.add(departmentQo);
        DataResult result = DataResult.success();
        return result;
    }

    @ApiOperation(value = "获取部门信息")
    @GetMapping("/department/{id}")
    public DataResult<DepartmentInfoVo> departmentInfo(@PathVariable("id") @NotNull Integer id) {
        DepartmentInfoVo departmentInfoVo = departmentService.getById(id);
        DataResult result = DataResult.success();
        result.setData(departmentInfoVo);
        return result;
    }

    @ApiOperation(value = "更新部门")
    @PatchMapping("/department/{id}")
    public DataResult updateDepartment(@PathVariable("id") @NotNull Integer id, @RequestBody @Valid DepartmentQo departmentQo) {
        departmentService.updateById(id, departmentQo);
        return DataResult.success();
    }

    @ApiOperation(value = "删除部门")
    @DeleteMapping("/department/{id}")
    public DataResult deleteDepartment(@PathVariable("id") Integer id) {
        departmentService.delete(id);
        return DataResult.success();
    }

    @ApiOperation(value = "获取部门树")
    @GetMapping("/department")
    public DataResult<List<Department>> listWithTree() {
        List<Department> departmentList = departmentService.listWithTree();
        DataResult result = DataResult.success();
        result.setData(departmentList);
        return result;
    }

}

